﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class RadioChoicesBLL : CommonBLL<RadioChoicesInfo, RadioChoicesDAL>
    {
        public static bool UpdateOrAdd(int rid, List<RadioChoicesInfo> list)
        {
            var toUpdate = list.Where(o => o.Id != 0);
            var toAdd = list.Where(o => o.Id == 0);
            //删除没有用的
            var re1 = DelAll(o => rid == o.Rid && !toUpdate.Select(o1 => o1.Id).Contains(o.Id));
            var re2 = Edit(toUpdate);
            var re3 = Add(toAdd);
            return re1 && re2 && re3;
        }

        public static IQueryable<RadioChoicesInfo> GetList(int rid)
        {
            var re = GetList();
            return re.Where(o => o.Rid == rid);
        }

        public static bool SetRadioKey(int rid, int keyId)
        {
            using (var dal = new RadioChoicesDAL())
            {
                return dal.SetRadioKey(rid, keyId);
            }
        }

        public static RadioChoicesInfo Get(int id)
        {
            using (var dal = new RadioChoicesDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool DelByRid(int rid)
        {
            using (var dal = new RadioChoicesDAL())
            {
                return dal.Delete(o => o.Rid == rid) != null;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new RadioChoicesDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
